package com.coderman.common.starter.tianqing.qlexpress.listener;

import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Properties;

/**
 * description: PaperWorkConfigServiceWrapper <br>
 * date: 2020/10/17 0:05 <br>
 * author: coderman <br>
 * version: 1.0 <br>
 */
@Service
public class QlExpressConfigServiceWrapper {
    @Autowired
    private NacosDiscoveryProperties nacosDiscoveryProperties;
    private ConfigService configService;
    /**
     * 通过原生Java方式获取ConfigService
     * @return
     * @throws NacosException
     */
    public synchronized ConfigService getConfigService() throws NacosException {
        if(configService == null){
            Properties properties = new Properties();
            properties.put(PropertyKeyConst.SERVER_ADDR, nacosDiscoveryProperties.getServerAddr());
            configService = NacosFactory.createConfigService(properties);
        }
        return configService;
    }
}
